﻿#include "productstandarditemmodel.h"

ProductStandardItemModel::ProductStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"商品ID"
         <<"型号"
        <<"型号拼音码"
       <<"名称"
      <<"名称拼音码"
     <<"单位"
    <<"下限"
    <<"上限"
    <<"库存数量"
    <<"净价"
    <<"金额"
    <<"三包库存数量"
    <<"货号"
    <<"品牌"
    <<"产地"
    <<"规格"
    <<"条型码"
    <<"备注"
    <<"指导价格1"
    <<"指导价格2"
    <<"一级类别"
    <<"二级类别";

    countIndex = 8;
    inPriceIndex = 9;
    sanbaoIndex = 11;

    setHorizontalHeaderLabels(header);
}

void ProductStandardItemModel::reset(){
    clear();
    products.clear();
    setHorizontalHeaderLabels(header);
}

void ProductStandardItemModel::insertProduct(Product &product)
{
    QList<QStandardItem*> rowData;

    rowData.append(new QStandardItem(product.productId));
    rowData.append(new QStandardItem(product.type));
    rowData.append(new QStandardItem(product.typePinyin));
    rowData.append(new QStandardItem(product.name));
    rowData.append(new QStandardItem(product.namePinyin));
    rowData.append(new QStandardItem(product.unit));
    rowData.append(new QStandardItem(QString("%1").arg(product.lowerLimit)));
    rowData.append(new QStandardItem(QString("%1").arg(product.upperLimit)));
    rowData.append(new QStandardItem(QString("%1").arg(product.count)));
    rowData.append(new QStandardItem(QString::number(product.inPrice, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(product.count*product.inPrice, 'f', 2)));
    rowData.append(new QStandardItem(QString("%1").arg(product.sanbao)));
    rowData.append(new QStandardItem(product.productNum));
    rowData.append(new QStandardItem(product.brand));
    rowData.append(new QStandardItem(product.originPlace));
    rowData.append(new QStandardItem(product.specifications));
    rowData.append(new QStandardItem(product.barCode));
    rowData.append(new QStandardItem(product.remark));
    rowData.append(new QStandardItem(QString::number(product.guidePrice1, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(product.guidePrice2, 'f', 2)));
    rowData.append(new QStandardItem(product.primaryDirectory));
    rowData.append(new QStandardItem(product.secondaryDirectory));

    products.append(&product);
    appendRow(rowData);
}

void ProductStandardItemModel::insertProduct(QList<Product*> products){
    for(Product *product:products){
        insertProduct(*product);
    }

    refreshTotalValue();
}

Product* ProductStandardItemModel::getProduct(int index){
    if(index<0 || index>=products.size()){
        return nullptr;
    }

    return products[index];
}

void ProductStandardItemModel::refreshTotalValue(){
    int totalCount = 0;
    int totalSanbao = 0;
    double totalInPrice = 0.0;

    for(int i=0; i<rowCount(); i++){
        QStandardItem *curCountItem = item(i, countIndex);
        QStandardItem *curSanbaoItem = item(i, sanbaoIndex);
        QStandardItem *curInPriceItem = item(i, inPriceIndex);
        if(curCountItem == nullptr || curSanbaoItem == nullptr || curInPriceItem == nullptr){
            break;
        }
        int curCount = curCountItem->text().toInt();
        int curSanbao = curSanbaoItem->text().toInt();
        double curInPrice = curInPriceItem->text().toDouble();
        totalCount += curCount;
        totalSanbao += curSanbao;
        totalInPrice += (curCount+curSanbao)*curInPrice;
    }

    emit updateTotalValue(totalCount, totalSanbao, totalInPrice);
}
